
let isString = (string) => {
    return Object.prototype.toString.call(string) === "[object String]";
}

let isEmptyString = (string) => {
    return string === undefined || !isString(string) || string.trim() === "";

}

let isArray = (array) => {
    if (!Array.isArray) {
        Array.isArray = (arg) => {
            return Object.prototype.toString.call(arg) === '[object Array]';
        };
    }
    return Array.isArray(array);
}


let isEmptyArray = (array) => {
    return isArray(array) && array.length === 0;
}


let isNumber = (obj) => {
    if (obj === undefined) {
        return false;
    }
    return typeof obj === "number";
}

let numberRange = (number, min, max) => {
    if (!isNumber(number) || !isNumber(min) || !isNumber(max)) {
        return false;
    }
    return number >= min && number <= max;
}

// 检查身份证
let checkIdentity = (identity) => {
    if(!isString(identity)){
        return false;
    }
    identity = identity.toUpperCase();
    let len, re;
    len = identity.length;
    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
    if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(identity))) {
        return false;
    }
    if (len === 18) {
        re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
        let arrSplit = identity.match(re);

        //检查生日日期是否正确
        let dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
        let bGoodDay;
        bGoodDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]));
        if (!bGoodDay) {
            return false;
        } else {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            let valnum;
            let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
            let nTemp = 0, i;
            for (i = 0; i < 17; i++) {
                nTemp += identity.substr(i, 1) * arrInt[i];
            }
            valnum = arrCh[nTemp % 11];
            return valnum === identity.substr(17, 1);

        }
    }
    return false;
}


let checkEmail = (email) =>{
    if(!isString(email)){
        return false;
    }
    let regExp = new RegExp("^[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?$");
    return regExp.test(email);
}

// 验证手机号码 11 位纯数字
let checkMobile = (mobile)=>{
    if(!isString(mobile)){
        return false;
    }
    let regExp = new RegExp("^\\d{11}$");
    return regExp.test(mobile);
}

// 验证电话号
let checkPhone = (phone)=>{
    if(!isString(phone)){
        return false;
    }
    let regExp = new RegExp("^\\d{3}-\\d{8}|\\d{4}-\\{7,8}$");
    return regExp.test(phone);
}

// 验证用户名
let checkUsername = (username,min = 6,max = 12)=>{
    if(!isString(username)){
        return false;
    }
    let pattern = "^[a-zA-Z0-9_]{"+min+","+max+"}$";
    let reg = new RegExp(pattern);
    return reg.test(username);
}

// 验证密码
let checkPassword = (password,min = 6,max = 12)=>{
    if(!isString(password)){
        return false;
    }
    let reg = new RegExp("^\\w{"+min+","+max+"}$");
    return reg.test(password);
}

// 获取密码强度
let getPasswordStrength = (password)=>{
    let level = 0;
    if(/[0-9]/.test(password)){
        level++
    }
    if(/[a-zA-Z]/.test(password)){
        level++
    }
    if(/\\W/.test(password) && !/[\u4e00-\u9fa5]/.test(password)){
        level++
    }
    return level;
}

export default {
    isString:isString,
    isEmptyString: isEmptyString,
    isEmptyArray: isEmptyArray,
    isArray: isArray,
    isNumber: isNumber,
    numberRange: numberRange,
    checkIdentity:checkIdentity,
    checkEmail:checkEmail,
    checkPhone:checkPhone,
    checkUsername:checkUsername,
    checkMobile:checkMobile,
    checkPassword:checkPassword,
    getPasswordStrength:getPasswordStrength
}